<?php
include_once ('class/class.koneksi.php');

class user{

	private $con;

    public function __construct() {
        $konek = new koneksi_db();
        $konek->koneksi();
        $this->con = $konek->con;
    }
	
	function login($spa,$password){ //function buat loginnya
		$hash_pass = hash('sha512',$password);
		$hashed = substr($hash_pass, 0, 45);
        $query=$this->con->prepare("select * from user where spa='$spa' and password='$password'");
		$query->execute();
		$data = $query->rowCount();
		return $data;
    }
	
	function cekUser($spa){ //function buat cek user
		$query=$this->con->query("SELECT kd_user, spa, email, roles FROM user WHERE spa='$spa'");
		$jml_data=$query->rowCount();
		if($jml_data==1){
			$hasil=$query->fetchAll();
			return $hasil;
		} else {
			return $jml_data;
		}
    }
	
	function Users(){ //function buat cek user
		$query=$this->con->query("SELECT a.spa spa, a.email email, b.roles roles FROM user a join roles b where a.roles = b.kd_roles");
		if($query){
            while($hasil=$query->fetch()){
                $user = new user();
                $user->spa=$hasil['spa'];
                $user->email=$hasil['email'];
                $user->roles=$hasil['roles'];
                $daftar_user[]=$user;
            }
            return $daftar_user;
        }
    }
	
	function hapusUser($kd_user) {
        $query=$this->con->prepare("delete from user where kd_user=$kd_user");
		$query->execute();
        $jml_data = $query->rowCount();
        return $jml_data;
    }
	
	public function editUser($spa, $password, $email){
		$date_edited = date("Y-m-d");
		$hash_pass = hash('sha512',$password);
		$hashed = substr($hash_pass, 0, 45);
		$query = $this->con->prepare("update user set password = ?, email = ?, date_edited = ? where spa = ?");
		$isi = array($hashed, $email, $date_edited, $spa);
		$query->execute($isi);
		var_dump($query);
		$jml_data = $query->rowCount();
		return $jml_data;
	}
	
	public function rekamUser($spa, $password, $email, $roles){
		$query = $this->con->prepare("Insert into user (spa, password, email, roles, date_created) values (?,?,?,?,?)");
		$date_created = date("Y-m-d");
		$hash_pass = hash('sha512',$password);
		$hashed = substr($hash_pass, 0, 45);
		$isi = array($spa, $hashed, $email, $roles, $date_created);
		$query->execute($isi);
		$jml_data = $query->rowCount();
		return $jml_data;
	}
	
	/*public function sesi($username){
		$query = $this->con->query("select kd_user, spa, username from users where username = $username");
	}*/
	

}


?>